package com.headStorm.httpuse;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.ZipUtil;
import com.headStorm.httpuse.httpsdemo.SafeExpireMap;
import javafx.collections.FXCollections;
import javafx.collections.MapChangeListener;
import javafx.collections.ObservableMap;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;

import javax.xml.transform.Source;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

public class HttpUse2 {

    private static String remoteInetAddr = "192.168.126.128";//需要连接的IP地址

    private static ExpiringMap<String, Integer> map = null;

    public static void main(String[] args) {
        // maxSize: 设置最大值,添加第11个entry时，会导致第1个立马过期(即使没到过期时间)
        // expiration：设置每个key有效时间10s, 如果key不设置过期时间，key永久有效。
        // variableExpiration: 允许更新过期时间值,如果不设置variableExpiration，不允许后面更改过期时间,一旦执行更改过期时间操作会抛异常UnsupportedOperationException
        // policy:
        //        CREATED: 只在put和replace方法清零过期时间
        //        ACCESSED: 在CREATED策略基础上增加, 在还没过期时get方法清零过期时间。
        //        清零过期时间也就是重置过期时间，重新计算过期时间.


//        map =  ExpiringMap.builder()
//                .maxSize(10000)
//                .expiration(10, TimeUnit.SECONDS)
//                .variableExpiration().expirationPolicy(ExpirationPolicy.CREATED).build();
//
//        ObservableMap<String, Integer> observableMap = FXCollections.observableMap(map);
//        observableMap.addListener(new MapChangeListener<String, Integer>() {
//            @Override
//            public void onChanged(Change<? extends String, ? extends Integer> change) {
//                System.out.println(change.getKey());
//            }
//        });
//
//        map.put("1", 3);
//        map.put("2", 4, 2, TimeUnit.SECONDS);
//
//        // 模拟线程等待...
//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//
//        System.out.println(map.size());
        //observableMapUse();
        mapUse();
    }

    public static void observableMapUse(){
            Map<String, String> test = new HashMap<>();
            ObservableMap<String, String> observableMap = FXCollections.observableMap(test);
            observableMap.addListener(
                    new MapChangeListener<String, String>() {
                        @Override
                        public void onChanged(Change<? extends String, ? extends String> change) {
                            System.out.println(observableMap.size());
                            System.out.println(change.getValueAdded());
                            ObservableMap<? extends String, ? extends String> map = change.getMap();
                        }
                    }
            );
            observableMap.put("1", "aaaaaa");
    }

    public static void mapUse(){
        ExpiringMap<Long, Long> rawDataMap =  ExpiringMap.builder()
                .maxSize(10000)
                .expiration(2, TimeUnit.SECONDS)
                .variableExpiration().expirationPolicy(ExpirationPolicy.CREATED).build();


        SafeExpireMap safeExpireMap = new SafeExpireMap<Long, Long>(1l, TimeUnit.SECONDS) {
            @Override
            protected void baseExpireEvent(Long key, Long val) {
            }

        };

        ConcurrentHashMap s = safeExpireMap.getDataMap();



        safeExpireMap.put(1l, 1l);
        safeExpireMap.put(2l, 2l);
        safeExpireMap.put(3l, 3l);
        safeExpireMap.put(4l, 4l);
        s.put(5l, 5l);




        Thread t1 = new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    Iterator<Map.Entry<Long, Long>> iterator = safeExpireMap.getDataMap().entrySet().iterator();
                    int size = safeExpireMap.getDataMap().size();
                    while (iterator.hasNext()){
                        Map.Entry<Long, Long> item = iterator.next();
                        System.out.println(item.getValue());
                        iterator.remove();
                    }
                    System.out.println(s.size());
                }
            }
        });

        t1.start();

//        Thread t2 = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true){
//                    safeExpireMap.put(System.currentTimeMillis(), System.currentTimeMillis());
//                    ThreadUtil.sleep(300);
//                }
//            }
//        });
//
//        t2.start();

//        safeExpireMap.put(1l, 1l);
//        safeExpireMap.put(2l, 2l);
//        safeExpireMap.put(3l, 3l);
//
//        System.out.println(safeExpireMap.getDataMap().size());
//        ThreadUtil.sleep(3000);
//        System.out.println(safeExpireMap.getDataMap().size());





    }
 }